<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once 'core/common/CommonEntryView.php';


class FileEntryView_list extends AppView
{
	
	var $template = 'list.html';
	var $template_popup = 'list_popup.html';
	
	
	function execute(&$obj, &$manager) {
		
		$tmpl = ($this->controller->getMoreParam('popup')) ? $this->template_popup :  $this->template;
		$tpl = &new tplTemplatez($this->template_dir . $tmpl);
		
		if($manager->setting['file_extract'] && $manager->isExtra('file')) {
			$tpl->tplSetNeededGlobal('filetext');
		}		
		
		// filter sql
		$categories = $manager->getCategoryRecords();
		$manager->setSqlParams($this->getFilterSql($manager, $categories));
		
		
		$buttons[] = 'insert';
		$bp = &$this->pageByPage($manager->limit, $manager->getCountRecords());
		$tpl->tplAssign('header', $this->commonHeaderList($bp->nav, $this->getFilter($manager, $categories), $buttons));
		
		// sort generate
		$sort = &$this->getSort();	
		$manager->setSqlParamsOrder($sort->getSql());
		
		// get records
		$rows =& $this->stripVars($manager->getRecords($bp->limit, $bp->offset));
		$ids = $manager->getValuesString($rows, 'id');
		
		// categories
		$categories = ($ids) ? $manager->getCategoryByIds($ids) : array();
		$categories = &$this->stripVars($categories);
		
		// roles to entry
		if($manager->isExtra('private')) {
			$tpl->tplSetNeededGlobal('private');
			
			$roles = ($ids) ? $manager->getMemberRoleById($ids, true) : array();
			$roles = &$this->stripVars($roles);
			
			$ids = $manager->getValuesString($rows, 'category_id'); 
			$category_roles = ($ids) ? $manager->cat->getMemberRoleById($ids, true) : array();
			$category_roles = &$this->parseCategoryRoles($category_roles);		
		}
		
		// list records
		foreach($rows as $row) {
			
			$obj->set($row);
			$obj->set('filesize', number_format($obj->get('filesize'), 0, '.', ' '));
			
			// private&roles
			if($row['private'] || $row['category_private'] && $manager->isExtra('private')) {

				$tpl->tplAssign('roles_msg', $this->getPrivateMsg(@$roles[$row['id']], @$category_roles[$row['category_id']]));
				$tpl->tplAssign($this->getColorsAndRolesMsg($row));
				$tpl->tplSetNeeded('row/if_private');
			}			
			
			$tpl->tplAssign('category', implode('<br>', $categories[$row['id']]));
			$tpl->tplAssign('formated_date', $this->getFormatedDate($row['ts']));
			$tpl->tplAssign('filetext', ($row['filetext']) ? $this->msg['yes_msg'] : '');
			//$tpl->tplAssign('download_num', $row['download_num']);
			
			$tpl->tplAssign($this->getViewListVars($obj->get('id'), $obj->get('active')));
			
			$tpl->tplParse(array_merge($obj->get(), $this->msg), 'row');
		}
		
		$tpl->tplAssign($this->msg);
		$tpl->tplAssign($sort->toHtml());
		
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function &parseCategoryRoles($category_roles) {
		return CommonEntryView::parseCategoryRoles($this->stripVars($category_roles), $this->msg);
	}	

	function getPrivateMsg($entry_roles, $category_roles) {
		return CommonEntryView::getPrivateMsg($entry_roles, $category_roles);
	}
	
	function getColorsAndRolesMsg($row) {
		return CommonEntryView::getColorsAndRolesMsg($row, $this->msg);
	}	
	
	
	function &getSort() {
		//$sort = new TwoWaySort();
		$sort = new OneWaySort($_GET);
		$sort->default_order = 1;
		$sort->setTitleMsg('asc',  $this->msg['sort_asc_msg']);
		$sort->setTitleMsg('desc', $this->msg['sort_desc_msg']);
		
		$sort->setSortItem('date_posted_msg', 'date', 'date_posted, e.id', $this->msg['date_posted_msg'], 2);
		
		$sort->setSortItem('title_msg', 'title', 'title', $this->msg['title_msg']);
		$sort->setSortItem('filename_msg', 'filename', 'filename', $this->msg['filename_msg']);
		$sort->setSortItem('filesize_msg', 'comments', 'comment_num', $this->msg['filesize_msg']);
		$sort->setSortItem('filesize_msg', 'filesize', 'filesize', $this->msg['filesize_msg']);
		$sort->setSortItem('filetype_msg', 'filetype', 'filetype', $this->msg['filetype_msg']);
		$sort->setSortItem('filetext_msg', 'filetext', 'filetext', $this->msg['filetext_msg']);
		$sort->setSortItem('download_num_msg', 'dowload', 'downloads', $this->msg['download_num_msg']);
		$sort->setSortItem('status_msg','status', 'active', array($this->msg['status_msg'], 3));
		//$sort->setSortItem('category_msg','cat', 'category_name', $this->msg['category_msg']);
		
		//$sort->getSql();
		//$sort->toHtml()
		return $sort;
	}
	
	
	function getFilter($manager, $categories) {

		$select = new FormSelect();
		$select->css_class = 'colorInput';
		$select->setSelectWidth(250);
		$select->setOnChangeSubmit(true);
		$select->setSelectName('filter');
		$select->setRange($manager->getCategorySelectRange($categories), 
		                  array('all'=>$this->msg['all_categories_msg']));
		
		$arr = $_GET; unset($arr[$select->select_name]);		

		
		$html = '<form action="" style="margin: 0px;">';
		$html .= http_build_hidden($arr);
		$html .= $select->select();
		$html .= '</form>';
		
		return $html;
	}
	
	
	function getFilterSql($manager, $categories) {
		
		// filter
		$arr = array();
		if(isset($_GET['filter']) && $_GET['filter'] != 'all') {
			
			$category_id = (int) $_GET['filter'];
			$child = array_merge($manager->getChilds($categories, $category_id), array($category_id));
			$child = implode(',', $child);
			
			$arr[] = "AND e_to_cat.category_id IN($child)";
		}
		
		return implode(" \n", $arr);
	}
	
	
	function getViewListVars($record_id = false, $active = false) {
		
		$row = parent::getViewListVars($record_id, $active);
		$row['filetext_link'] = $this->getActionLink('text', $record_id);
		$row['file_link'] = $this->getActionLink('file', $record_id);
		
		return $row;
	}
}
?>